//package com.qingguo.MD5.verifyMd5
//
//import java.security.MessageDigest
//import org.apache.spark.sql.{DataFrame, Row, SparkSession}
//
//object VerifyMd5 {
//  def main(args: Array[String]): Unit = {
//    val spark = SparkSession.builder()
////      .master("local")
//      .config("spark.sql.sources.partitionOverwriteMode","dynamic")
//      .config("hive.exec.dynamic.partition.mode","nonstrict")
////      .config("spark.sql.shuffle.partitions","20")
//      .config("spark.sql.crossJoin.enabled",true)
//      .appName("VerifyMd5").enableHiveSupport().getOrCreate()
//
//    val tableName = args(0)
//    var whereExp = ""
//    if (args.length > 1) {
//      var dt = args(1)//bizDate
//      whereExp = " where dt = '" + dt + "'"
//    }
//    // instantiation MD5 class
//    val md5 = new Md5Verifier()
////    val dt = "20200610"
//    // hive table data Df
//    val sqlFullData = spark.sql("SELECT * from " + tableName + whereExp)
////    val sqlData = sqlFullData.drop("dt","md5_coding")
////    val sqlData = sqlFullData.drop("md5_coding")
//    val sqlData = sqlFullData
////    val sqlDate = sqlFullData.select("dt").limit(1)
////    sqlDate.cache()
//    val rowRdd = sqlData.rdd.map(row=>{
////      val id = row.getDecimal(0)
//////      println("********************** id=" + id)
////      val md5Data = row.mkString("")
//////      val md5Data = row.mkString("\001")
////        .replace("null","")
////        .split("\001")
////        .map(replaceDouble(_))
//
//      val dtIdx = row.fieldIndex("dt")
//      val md5Idx = row.fieldIndex("md5_coding")
////      val rowData = new Array[Object](row.length)
////      val rowData = Row.unapplySeq(row).get.map(_.asInstanceOf[String]).toBuffer
//      val rowDataArray = row.toSeq.toArray
//      val rowDataSb = new StringBuilder
//      for( i <- 0 until row.length) {
//        val value = String.valueOf(row.get(i))
//        if (i != dtIdx && i != md5Idx) {
//          if (!"null".equals(value)) {
//            rowDataSb.append(md5.replaceDouble(value))
//          }
//        }
//      }
//      val md5Val = md5.md5Verify(rowDataSb.toString())
////  println("**************************************** md5Val=" + md5Val)
//      rowDataArray(md5Idx) = md5Val
////      val newRow = new GenericRowWithSchema(rowDataArray, row.schema)
//      val newRow = Row.fromSeq(rowDataArray)
//      newRow
////      (row,md5Data)
////    }).map(x=>(x._1,md5.Md5Verify(x._2.mkString("\001"))))
////    }).map(x=>(x._1,md5.Md5Verify(x._2.mkString(""))))
////    }).map(x=>(x._1,""))
//    })
////      .toDF("id","md5_coding")
////      .toDF(sqlFullData.schema.fieldNames: _*)
//
////    val md5TableData = rowRdd.toDF()
////    println("********************** write back to hive")
////    sqlData.join(md5TableData,Seq("id"),"left").join(sqlDate)
////      .write.mode("overwrite").insertInto(tableName)
//    spark.createDataFrame(rowRdd, sqlData.schema)
//      .write.mode("overwrite").insertInto(tableName)
//
//  }
//
//
//  class Md5Verifier(removeSpace: Boolean = true, delimiter: String = "") extends Serializable {
//    var _delimiter: String = delimiter
//    var _removeSpace: Boolean = removeSpace
//
//    def md5Verify(content: String): String = {
//      _md5Verify(trim(content))
//    }
//
//    def trim(content: String): String = {
//      var str = content
//      if (!_delimiter.isEmpty) {
//        str = removeStr(str, _delimiter)
//      }
//      if (_removeSpace) {
//        str = removeStr(str, " ")
//      }
//      str
//    }
//
//    private def _md5Verify(content: String): String = {
//      val md5 = MessageDigest.getInstance("MD5")
//      val dataMd5 = md5.digest((content).getBytes)
//      dataMd5.map("%02x".format(_)).mkString
//    }
//
//    def replaceDouble(content: String): String = {
//      var str = content
//      if (content.indexOf(".")>0) {
//        str = content.replaceAll("0+?$", "").replaceAll("[.]$", "")
//      }
//      str
//    }
//
//    private def removeStr(content: String, removeStr: String): String ={
//      return content.replace(removeStr, "")
//    }
//  }
//
//}
//
